package com.goods.controller.business;

import com.goods.business.service.CategoryService;
import com.goods.common.error.BusinessException;
import com.goods.common.model.business.ProductCategory;
import com.goods.common.response.ResponseBean;
import com.goods.common.vo.business.ProductCategoryTreeNodeVO;
import com.goods.common.vo.business.ProductCategoryVO;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Description: 商品产品类别
 * @Author: Mr.Cao
 * @DateTime: 2021/11/8 16:50
 * BMW F750GS
 **/
@RestController
@RequestMapping("business/productCategory")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
    //gmall.com
    //百度.com
    /**
     * 分页查询商品分类
     * */
    //http://www.localhost:8989/business/productCategory/categoryTree?pageNum=1&pageSize=5
    @GetMapping("categoryTree")
    public ResponseBean categoryTree(@RequestParam Map map){
        PageVO<ProductCategoryTreeNodeVO> categoryList =  categoryService.categoryTree(map);
        if (categoryList!=null) {
            return ResponseBean.success(categoryList);
        } else{
            return ResponseBean.error("数据为空");
        }
    }

    /**
     * 添加商品时显示父级分类的名称
     * */
    //http://www.localhost:8989/business/productCategory/getParentCategoryTree
    @GetMapping("getParentCategoryTree")
    public ResponseBean getParentCategoryTree(){
       List parentCategoryName =  categoryService.getParentCategoryTree();
        return ResponseBean.success(parentCategoryName);
    }
    /**
     *添加分类
     */
    //http://www.localhost:8989/business/productCategory/add
    @PostMapping("add")
    public ResponseBean add(@RequestBody ProductCategoryVO productCategoryVO){
        try {
            categoryService.add(productCategoryVO);
            return ResponseBean.success();
        } catch (BusinessException e) {
            e.printStackTrace();
            return ResponseBean.error(e.getErrorMsg());
        }

    }
    /**
     * 删除分类
     * */
    @DeleteMapping("delete/{categoryId}")
    public ResponseBean delete(@PathVariable long categoryId){
        try {
            categoryService.delete(categoryId);
            return ResponseBean.success();
        } catch (BusinessException e) {
            //e.printStackTrace();
            //System.out.println("e = " + e.getErrorMsg());
            return ResponseBean.error(e.getErrorMsg());
        }
    }
    /**
     * 编辑分类时数据回显
     * */
    //http://www.localhost:8989/business/productCategory/edit/33
    @GetMapping("edit/{categoryId}")
    public ResponseBean edit(@PathVariable long categoryId){
        ProductCategory productCategory =  categoryService.edit(categoryId);
        return ResponseBean.success(productCategory);
    }
    /**
     * 更新分类信息
     * */
    //http://www.localhost:8989/business/productCategory/update/33
    @PutMapping("update/{categoryId}")
    public ResponseBean update(@PathVariable long categoryId, @RequestBody ProductCategoryVO productCategoryVO){
        categoryService.update(categoryId,productCategoryVO);
        return ResponseBean.success();
    }
}
